<template>
    <div>
        <div>
            <el-input
                    size="mini"
                    placeholder="添加职称..."
                    prefix-icon="el-icon-plus"
                    v-model="jl.name"
                    class="addJlInput"
                   >
            </el-input>
            <el-select v-model="jl.titleLevel" placeholder="职称等级" style="margin-right: 8px" size="mini">
                <el-option
                        v-for="item in titleLevels"
                        :key="item"
                        :label="item"
                        :value="item"
                        :disabled="item.disabled">
                </el-option>
            </el-select>
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="addJobLevel">添加</el-button>

        </div>
        <div>
            <el-table
                    @selection-change="handleSelectionChange"
                    :data="jls"
                    border
                    stripe
                    class="jlsManaStyle"
                    size="mini"
                    style="width: 70%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职称名称"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="titleLevel"
                        label="职称等级"
                        width="120">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        label="创建时间"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="enabled"
                        label="是否启用"
                        width="120"
                        >
                    <template slot-scope="scope">
                        <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag type="danger" v-else>未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showEditView(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button type="danger" size="mini" style="margin-top:8px" :disabled="multipleSelection.length==0" @click="deleteMany">批量删除</el-button>
        </div>
        <el-dialog
                title="修改职称"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <table>
                    <tr>
                        <td>
                            <el-tag>
                                职位名称
                            </el-tag>
                        </td>
                        <td>
                            <el-input class="updateJobInput" size="mini" v-model="updateJob.name"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>
                                职称等级
                            </el-tag>
                        </td>
                        <td>
                            <el-select v-model="updateJob.titleLevel" placeholder="职称等级" style="margin-left: 8px" size="mini">
                                <el-option
                                        v-for="item in titleLevels"
                                        :key="item"
                                        :label="item"
                                        :value="item"
                                        :disabled="item.disabled">
                                </el-option>
                            </el-select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>
                                是否启用
                            </el-tag>
                        </td>
                        <td>
                            <el-switch
                                    style="margin-left: 10px;display: block"
                                    v-model="updateJob.enabled"
                                    active-text="启用"
                                    inactive-text="禁用"
                                    active-color="#13ce66"
                                    inactive-color="#ff4949">
                            </el-switch>
                        </td>
                    </tr>
                </table>
            </div>

            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
    <el-button type="primary" @click="doUpdate" size="mini">确 定</el-button>
  </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "JobLevelMana",
        data(){
            return{
                jl:{
                    name:'',
                    titleLevel:'',

                },
                updateJob:{
                    name:'',
                    titleLevel:'',
                    enabled:true,


                },

                dialogVisible:false,
                multipleSelection:[],
                jls:[],
                titleLevels:[
                    '正高级',
                    '副高级',
                    '中级',
                    '初级',
                    '员级'

                ]
            }
        },
        mounted() {
            this.initJobLevel();
        },
        methods:{
            deleteMany(){
                this.$confirm('是否删除【' +this.multipleSelection.length+ '】条记录？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids='?';
                    this.multipleSelection.forEach(item=>{
                        ids+='ids='+item.id+'&';
                    })
                    this.deleteRequest("/system/basic/job/"+ids).then(resp=>{
                        if (resp){
                            this.initJobLevel();
                        }

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSelectionChange(val){
                this.multipleSelection=val;
            },
            initJobLevel(){
                this.getRequest("/system/basic/job/").then(resp=>{
                    if (resp){
                        this.jls=resp;
                    }
                })
            },
            addJobLevel(){
                if (this.jl.name) {
                    this.postRequest("/system/basic/job/", this.jl).then(resp => {
                        if (resp) {
                            this.initJobLevel();
                            this.jl.name = '';
                        }
                    })
                }else{
                    this.$message.error('职位名称不可以为空，请重新输入！');

                }

            },
            handleDelete(index,data){
                this.$confirm('是否删除【'+data.name+'】？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/job/"+data.id).then(resp=>{
                        if (resp){
                            this.initJobLevel();
                        }

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            showEditView(index,data){
                Object.assign(this.updateJob,data);
                this.dialogVisible = true;
            },
            doUpdate(index,data){
                this.putRequest("/system/basic/job/",this.updateJob).then(resp=>{
                    if (resp){
                        this.initJobLevel();
                        this.dialogVisible=false;
                    }
                })

            }
        }
    }
</script>

<style>
    .addJlInput{
        width: 200px;
        margin-right: 8px;
    }
    .jlsManaStyle{
        margin-top: 8px;
    }
    .updateJobInput{
        width: 200px;
        margin-left: 8px;
        margin-top: 8px;
    }
</style>